/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.florent37.github.com.expansionpanel.slice;

import com.florent37.github.com.expansionpanel.ExpansionHeader;
import com.florent37.github.com.expansionpanel.ExpansionLayout;
import com.florent37.github.com.expansionpanel.ResourceTable;
import com.florent37.github.com.expansionpanel.viewgroup.ExpansionLayoutCollection;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.render.Canvas;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * lipeiquan
 *
 * @since 2021-05-06
 */
public class ExpansionPanelSampleAbilityRecyclerSlice extends AbilitySlice {
    private ListContainer mIdListContainer;
    private final int mCount = 30;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_expansion_panel_sample_recycler);
        initListContainer();
    }

    private void initListContainer() {
        mIdListContainer = (ListContainer) findComponentById(ResourceTable.Id_listContainer);
        SampleItemProvider sampleItemProvider = new SampleItemProvider();
        List<Object> list = new ArrayList<>();
        for (int i = 0; i < mCount; i++) {
            list.add(new Object());
        }
        sampleItemProvider.setItems(list);
        mIdListContainer.setItemProvider(sampleItemProvider);
        mIdListContainer.addDrawTask(new Component.DrawTask() {
            @Override
            public void onDraw(Component component, Canvas canvas) {
                component.setHeight(ComponentContainer.LayoutConfig.MATCH_CONTENT);
                component.setHeight(ComponentContainer.LayoutConfig.MATCH_PARENT);
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    /**
     * lipeiquan
     *
     * @since 2021-05-06
     */
    public static final class SampleItemProvider extends BaseItemProvider {
        private final List<Object> list = new ArrayList<>();
        private final ExpansionLayoutCollection expansionsCollection = new ExpansionLayoutCollection();
        private final HashMap<Integer, Component> mComponents = new HashMap<>();

        /**
         * 初始化
         */
        public SampleItemProvider() {
            expansionsCollection.openOnlyOne(true);
        }

        /**
         * 存放数据
         *
         * @param items
         */
        public void setItems(List<Object> items) {
            this.list.addAll(items);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
            final Component cpt;
            Component component = mComponents.get(position);
            if (component != null) {
                return component;
            }
            cpt = LayoutScatter.getInstance(componentContainer.getContext()).parse(ResourceTable.Layout_expansion_panel_sample_panel, componentContainer, false);
            ((ExpansionHeader) ((ComponentContainer) cpt).getComponentAt(0)).init(ResourceTable.Id_expansionLayout, true, ResourceTable.Id_headerIndicator, 0, 0);
            ((ExpansionLayout) ((ComponentContainer) cpt).getComponentAt(1)).collapse(false);
            ((ExpansionLayout) ((ComponentContainer) cpt).getComponentAt(1)).init(300);
            expansionsCollection.add((ExpansionLayout) ((ComponentContainer) cpt).getComponentAt(1));
            mComponents.put(position, cpt);
            return cpt;
        }
    }
}
